package com.sunnada.edu.business.attendance.controller;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sunnada.edu.business.attendance.bean.AttenRecord;
import com.sunnada.edu.business.attendance.bean.Attendance;
import com.sunnada.edu.business.attendance.bean.PieceQuery;
import com.sunnada.edu.business.attendance.service.AttendanceService;
import com.sunnada.edu.business.attendance.service.PieceQueryService;
import com.sunnada.edu.system.pub.common.BasicController;
import com.sunnada.edu.system.pub.dao.Page;
import com.sunnada.edu.system.pub.service.PubMethods;
import com.sunnada.edu.system.pub.util.Utils;

/***
 * explain 计件查询
 * 
 * @author zhenglx
 */
@Controller
@RequestMapping("/pieceQueryController")
public class PieceQueryController extends BasicController {
	@Resource
	PieceQueryService pieceQueryService;
	@Resource
	AttendanceService attendanceService;
	/** 计件查询首页 */
	@RequestMapping("/pieceQuery")
	public String pieceQuery(HttpServletRequest request) {
		String curr = request.getParameter("curr");
		request.setAttribute("curr", curr);
		// 当前路径
		String moduleId = request.getParameter("moduleId");
		request.setAttribute("pathValue", PubMethods.getPathValue(moduleId));
		List<?> g = attendanceService.getWorkerGroup(null);
		request.setAttribute("groupList", g);
		return "business/attendance/pieceQuery";
	}

	/** 计件查询 */
	@RequestMapping("/pieceQuerySearch")
	public void pieceQuerySearch(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, PieceQuery.class, params);
		Page page = new Page();
		page.setRowCount(pieceQueryService.getPieceQueryCots(params));
		page.setPageMap(request, page, params);
		List<?> userList = pieceQueryService.findPieceQuery(params);
		JSONObject json = new JSONObject();
		json.put("list", userList);
		json.put("params", params);
		printInfo(response, json);
	}

	/** 考勤导出 */
	@RequestMapping("/exportPiece")
	public void exportPiece(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, PieceQuery.class, params);
		try {
			pieceQueryService.exportPiece(params, request, response);
		} catch (Exception e) {
			printInfo(response, "<script>alert('导出失败，请稍后重试！');window.history.go(-1);</script>");
		}
	}
	
	
	/**跳转审批详情页*/
	@RequestMapping("/goDetailPage")
	public String goDetailPage(HttpServletRequest request) { 
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, null, params);
		request.setAttribute("recId", params.get("recId"));
		return "business/pieceApply/piece_view";
	}
	
	
	/** 考勤记录修改页面 */
	@RequestMapping("/attenRecordUpdate")
	public String attendanceUpdate(HttpServletRequest request) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		return "business/attendance/attendance_update";
	}
	
	/** 考勤记录保存功能 */
	@RequestMapping("/attenRecordSave")
	public void userSave(HttpServletRequest request, HttpServletResponse response) {
		HashMap<String, String> params = new HashMap<String, String>();
		setReqParams(request, Attendance.class, params);
		// 验证用户账号是否重复
		String recId = params.get("recId");
		if (Utils.isNotNull(recId)) {
			msg = pieceQueryService.attendanceUpdate(params);
		} else {
			recId = UUID.randomUUID().toString();
			params.put("recId", recId);
			msg = pieceQueryService.attenRecordAdd(params);
		}
		printInfo(response, msg);
	}

	
}
